/*
 * Author: Roy Stegers & Gino Biervliet
 *
 * Upload functien: This function wil load data to client mmap
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <string.h>
#include <semaphore.h>
#include "hfile.h"

int upload(int descClient, clientInfo client[])
{
    printf("in upload\n");//Debug

    long unsigned amountWritten = 0, recvAmount = 0;

    printf("entered uploadmap entered \n");//Debug

    //do while construction to check condition at end of loop
    while(1)
    {
        bzero(client[descClient].tempBuffer, CHUNKSIZE);//Bzero again to make sure there is no extra data.

        //Set position of filedesc for read (iMapFd) to 0(beginning, seek_set). Makes sure you read from the beginning of the filedesc(SEEK_SET + 0 beginning of file).
        lseek(client[descClient].iMapFd, 0, SEEK_SET);

        //Read data and put in tempBuffer, size of chunksize
        recv(descClient, client[descClient].tempBuffer, CHUNKSIZE,0);

        //Count amount of bytes in temBuffer
        recvAmount =strlen(client[descClient].tempBuffer);

        //If amount is equal to CHUNKSIZE
        if(recvAmount == CHUNKSIZE)
        {
            //Bzero mmap the size of chucksize, empty it.
            bzero(client[descClient].cMapFile, CHUNKSIZE);
               
            printf("Inhoud client mmap voor write: %s\n", client[descClient].cMapFile);//Debug

           //Recv data from client and put this in tempBuffer
            if(amountWritten = write(client[descClient].iMapFd, client[descClient].tempBuffer, CHUNKSIZE)< 0)
            {
                perror("incorrect amount of bytes!\n");
            }

            printf("Inhoud client mmap na write: %s\n", client[descClient].cMapFile);//Debug

            //post semFull, Say Mmap is full to FSP
            if(sem_post(client[descClient].semaphoreFull) < 0)
            {
                perror("upload semEmpty\n");
            }
            printf("posted semFull\n");//Debug

            //Empty tempBuffer for next data segment
            bzero(client[descClient].tempBuffer, CHUNKSIZE);
            printf("waiting for semaphoreEmpty...\n");//Debug

            //Wait for semEmpty Mmap, to know that we can write the next bit in client mmf
            if(sem_wait(client[descClient].semaphoreEmpty) < 0)
            {
                perror("upload semEmpty\n");
            }

            //Empty client mmap for next data segment.
            bzero(client[descClient].cMapFile, CHUNKSIZE);
        }

        else
        {
            //Make sure that there is no data in te mmap
            bzero(client[descClient].cMapFile, CHUNKSIZE);

            printf("Inhoud client mmap voor write: %s\n", client[descClient].cMapFile);//Debug

           //Write tempBuffer to clientMmap
            if(amountWritten = write(client[descClient].iMapFd, client[descClient].tempBuffer, recvAmount)< 0)
            {
                perror("incorrect amount of bytes!\n");
            }   

            printf("Inhoud client mmap na write: %s\n", client[descClient].cMapFile);//Debug


            //post semFull, Say Mmap is full to FSP
            if(sem_post(client[descClient].semaphoreFull) < 0)
            {
                perror("upload semEmpty\n");
            }
            printf("posted semFull\n");//Debug

            //Empty tempBuffer 
            bzero(client[descClient].tempBuffer, CHUNKSIZE);

            printf("waiting for semaphoreEmpty...\n");//Debug

            //Wait for semEmpty Mmap, to know that we can write the next bit in client mmf
            if(sem_wait(client[descClient].semaphoreEmpty) < 0)
            {
                perror("upload semEmpty\n");
            }

            //Empty client mmap
            bzero(client[descClient].cMapFile, CHUNKSIZE);

            break;
        }

    }
   
        //Post semEmpty
        if(sem_post(client[descClient].semaphoreEmpty) < 0)
        {
            perror("post semEmpty\n");
        }

        printf("exit upload\n");
        
    return 0;
}